/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package loginview;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Font;
import java.io.FileOutputStream;
import static java.lang.Thread.sleep;
import java.sql.*;
import java.util.Calendar;
import javax.swing.*;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.proteanit.sql.DbUtils;

public class TotalViewFrame extends javax.swing.JFrame {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    
    ResultSet rs1 = null;
    PreparedStatement pst1 = null;
    /**
     * Creates new form TotalViewFrame
     */
    public TotalViewFrame() {
        initComponents();
        conn = JavaConnect.ConnecrDb();
        Update_IncomeTable();
        Update_OutcomeTable();
        IncomePrice();
        OutcomePrice();
        TotalPrice();
        CurrentDate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        homeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lbl_date = new javax.swing.JLabel();
        lbl_time = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        incomeTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        outcomeTable = new javax.swing.JTable();
        reportButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lbl_income = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lbl_outcome = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        lbl_total = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocation(new java.awt.Point(120, 20));
        setPreferredSize(new java.awt.Dimension(1050, 765));
        setSize(new java.awt.Dimension(900, 620));

        jToolBar1.setRollover(true);

        homeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/loginview/green-home-icon.png"))); // NOI18N
        homeButton.setText("Home");
        homeButton.setFocusable(false);
        homeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        homeButton.setMaximumSize(new java.awt.Dimension(75, 60));
        homeButton.setPreferredSize(new java.awt.Dimension(75, 60));
        homeButton.setSize(new java.awt.Dimension(75, 60));
        homeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(homeButton);

        jLabel1.setMaximumSize(new java.awt.Dimension(640, 55));
        jLabel1.setPreferredSize(new java.awt.Dimension(640, 55));
        jToolBar1.add(jLabel1);

        lbl_date.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_date.setForeground(new java.awt.Color(51, 51, 51));
        lbl_date.setText("Date");
        lbl_date.setMaximumSize(new java.awt.Dimension(150, 55));
        lbl_date.setPreferredSize(new java.awt.Dimension(150, 55));
        jToolBar1.add(lbl_date);

        lbl_time.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_time.setForeground(new java.awt.Color(51, 51, 51));
        lbl_time.setText("Time");
        lbl_time.setMaximumSize(new java.awt.Dimension(150, 55));
        lbl_time.setPreferredSize(new java.awt.Dimension(150, 55));
        jToolBar1.add(lbl_time);

        incomeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(incomeTable);

        outcomeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(outcomeTable);

        reportButton.setText("Report");
        reportButton.setMaximumSize(new java.awt.Dimension(97, 30));
        reportButton.setPreferredSize(new java.awt.Dimension(97, 30));
        reportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Income", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N

        lbl_income.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_income.setForeground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbl_income, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lbl_income, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 4, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Outcome", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N

        lbl_outcome.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_outcome.setForeground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbl_outcome, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(lbl_outcome, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 4, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Total", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N

        lbl_total.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_total.setForeground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbl_total, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(lbl_total, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 4, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1038, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(reportButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(217, 217, 217)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(reportButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(215, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Update_IncomeTable(){
    String sql = "SELECT * FROM Income";
    try{
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    incomeTable.setModel(DbUtils.resultSetToTableModel(rs));
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    finally {
    try{
    rs.close(); 
    pst.close(); }
    catch(Exception e) { } }
}
    
    private void Update_OutcomeTable(){
        String sql = "SELECT * FROM Outcome";
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            outcomeTable.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        finally{
            try{
                rs.close();
                pst.close();}
                catch(Exception e) {}
            }
    }
    
    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        MainViewFrame mainFrame = new MainViewFrame();
        mainFrame.setVisible(true);
        dispose();
        

    }//GEN-LAST:event_homeButtonActionPerformed

    public void IncomePrice(){
        
        try{
            String sql = "SELECT sum(Money) FROM Income";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            Double add1 = rs.getDouble("sum(Money)");
            
            
            lbl_income.setText(add1.toString());
            
        } 
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
    public void OutcomePrice(){
        try{
           String sql = "SELECT sum(Maintenance), sum(Salary) FROM Outcome";
           
           
           pst = conn.prepareStatement(sql);
           rs = pst.executeQuery();
            Double add1 = rs.getDouble("sum(Maintenance)");
            Double add2 = rs.getDouble("sum(Salary)");
            Double result = (add1+add2);
            lbl_outcome.setText(result.toString());
            
        } catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        
//         try{
//            String sql = "SELECT sum(Money) FROM Income";
//            pst = conn.prepareStatement(sql);
//            rs = pst.executeQuery();
//            Double add1 = rs.getDouble("sum(Money)");
//            
//            
//            lbl_income.setText("Income : "+add1.toString());
//            
//        } 
//        catch(Exception e){
//            JOptionPane.showMessageDialog(null, e);
//        }
    }
    
    public void TotalPrice(){
        try{
            String sql = "SELECT sum(Maintenance), sum(Salary) FROM Outcome";
           String sql1 = "SELECT sum(Money) FROM Income";
           
           
           pst = conn.prepareStatement(sql);
           rs = pst.executeQuery();
           
           pst1 = conn.prepareStatement(sql1);
           rs1 = pst1.executeQuery();
           Double addi = rs1.getDouble("sum(Money)"); 
           
           
            Double add1 = rs.getDouble("sum(Maintenance)");
            Double add2 = rs.getDouble("sum(Salary)");
            Double result = addi-(add1+add2);
            lbl_total.setText(result.toString());
            
        } 
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
    private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportButtonActionPerformed
        try{
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("Report.pdf"));
            document.open();
            Image image = Image.getInstance("logo.png");
            document.add(image);
            
            document.add(new Paragraph("Report Of Total", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.RED)));
            document.add(new Paragraph(new Date().toString()));
            document.add(new Paragraph("------------------------------------------------------"));
            
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell = new PdfPCell(new Paragraph("ID"));
            cell.setColspan(1);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBackgroundColor(BaseColor.GREEN);
            table.addCell(cell);
            table.addCell("item1");
           
           
            PdfPCell cell1 = new PdfPCell(new Paragraph("Name"));
            cell1.setColspan(1);
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell1.setBackgroundColor(BaseColor.GREEN);
            table.addCell(cell1);
            table.addCell("item1");
            
            
            
            document.add(table);
            
            
            List list = new List(true, 20);
            list.add("First Item");
            list.add("Second Item");
            list.add("Third Item");
            list.add("Fourth Item");
            document.add(list);
            
            document.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

    }//GEN-LAST:event_reportButtonActionPerformed

    public void CurrentDate(){
       Thread clock =new Thread(){
       public void run(){
           for(;;){
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        lbl_date.setText("Date "+day+"/"+(month+1)+"/"+year);
        
       int second = cal.get(Calendar.SECOND);
       int minute = cal.get(Calendar.MINUTE);
       int hour = cal.get(Calendar.HOUR_OF_DAY);
       lbl_time.setText("Time "+hour+":"+minute+":"+second);
               try {
                   sleep(1000);
               } catch (InterruptedException ex) {
                   Logger.getLogger(MainViewFrame.class.getName()).log(Level.SEVERE, null, ex);
               }
           }
       }
       
       };
        clock.start();
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TotalViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TotalViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TotalViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TotalViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TotalViewFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton homeButton;
    private javax.swing.JTable incomeTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lbl_date;
    private javax.swing.JLabel lbl_income;
    private javax.swing.JLabel lbl_outcome;
    private javax.swing.JLabel lbl_time;
    private javax.swing.JLabel lbl_total;
    private javax.swing.JTable outcomeTable;
    private javax.swing.JButton reportButton;
    // End of variables declaration//GEN-END:variables
}
